package org.dromara.mp.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustryEnum;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.dromara.common.core.domain.R;
import org.dromara.common.web.core.BaseController;
import org.dromara.mp.annotation.SwitchoverCheck;
import org.dromara.mp.service.WxMpTemplateService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 微信公众号模板消息控制器
 *
 * @author ruoyi
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wx/mp/template/{appid}")
public class WxMpTemplateController extends BaseController {

    private final WxMpService wxMpService;
    private final WxMpTemplateService wxMpTemplateService;

    /**
     * 发送模板消息
     *
     * @param appid           公众号appid
     * @param templateMessage 模板消息
     * @return 发送结果
     */
    @PostMapping("/send")
    @SwitchoverCheck
    public R<String> sendTemplateMessage(@PathVariable String appid,
                                         @RequestBody WxMpTemplateMessage templateMessage) {
        try {
            String msgId = wxMpTemplateService.sendTemplateMessage(templateMessage);
            return R.ok(msgId);
        } catch (Exception e) {
            log.error("发送模板消息失败: {}", e.getMessage(), e);
            return R.fail("发送模板消息失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有模板列表
     *
     * @param appid 公众号appid
     * @return 模板列表
     */
    @GetMapping("/list")
    @SwitchoverCheck
    public R<List<WxMpTemplate>> getAllTemplates(@PathVariable String appid) {
        try {
            List<WxMpTemplate> templates = wxMpTemplateService.getAllTemplates();
            return R.ok(templates);
        } catch (Exception e) {
            log.error("获取模板列表失败: {}", e.getMessage(), e);
            return R.fail("获取模板列表失败: " + e.getMessage());
        }
    }

    /**
     * 删除模板
     *
     * @param appid      公众号appid
     * @param templateId 模板ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{templateId}")
    @SwitchoverCheck
    public R<Void> deleteTemplate(@PathVariable String appid, @PathVariable String templateId) {
        try {
            boolean result = wxMpTemplateService.deleteTemplate(templateId);
            return result ? R.ok() : R.fail("删除模板失败");
        } catch (Exception e) {
            log.error("删除模板失败: {}", e.getMessage(), e);
            return R.fail("删除模板失败: " + e.getMessage());
        }
    }

    /**
     * 获取行业信息
     *
     * @param appid 公众号appid
     * @return 行业信息
     */
    @GetMapping("/industry")
    @SwitchoverCheck
    public R<WxMpTemplateIndustry> getIndustry(@PathVariable String appid) {
        try {
            WxMpTemplateIndustry industry = wxMpService.getTemplateMsgService().getIndustry();
            return R.ok(industry);
        } catch (Exception e) {
            log.error("获取行业信息失败: {}", e.getMessage(), e);
            return R.fail("获取行业信息失败: " + e.getMessage());
        }
    }

    /**
     * 设置行业信息
     *
     * @param appid       公众号appid
     * @param industryId1 行业代码1
     * @param industryId2 行业代码2
     * @return 设置结果
     */
    @PostMapping("/industry")
    @SwitchoverCheck
    public R<Void> setIndustry(@PathVariable String appid,
                               @RequestParam String industryId1,
                               @RequestParam String industryId2) {
        try {
            WxMpTemplateIndustry wxMpTemplateIndustry = new WxMpTemplateIndustry();
            wxMpTemplateIndustry.setPrimaryIndustry(WxMpTemplateIndustryEnum.valueOf(industryId1));
            wxMpTemplateIndustry.setSecondIndustry(WxMpTemplateIndustryEnum.valueOf(industryId2));
            wxMpService.getTemplateMsgService().setIndustry(wxMpTemplateIndustry);
            return R.ok();
        } catch (Exception e) {
            log.error("设置行业信息失败: {}", e.getMessage(), e);
            return R.fail("设置行业信息失败: " + e.getMessage());
        }
    }

    /**
     * 发送通知消息
     *
     * @param appid      公众号appid
     * @param openid     用户openid
     * @param templateId 模板ID
     * @param url        跳转URL
     * @param data       消息数据
     * @return 发送结果
     */
    @PostMapping("/send-notification")
    @SwitchoverCheck
    public R<String> sendNotification(@PathVariable String appid,
                                      @RequestParam String openid,
                                      @RequestParam String templateId,
                                      @RequestParam String url,
                                      @RequestBody Map<String, String> data) {
        try {
            String msgId = wxMpTemplateService.sendNotificationMessage(openid, templateId, url, data);
            return R.ok(msgId);
        } catch (Exception e) {
            log.error("发送通知消息失败: {}", e.getMessage(), e);
            return R.fail("发送通知消息失败: " + e.getMessage());
        }
    }

    /**
     * 发送订单消息
     *
     * @param appid  公众号appid
     * @param openid 用户openid
     * @param data   订单数据
     * @return 发送结果
     */
    @PostMapping("/send-order")
    @SwitchoverCheck
    public R<String> sendOrderMessage(@PathVariable String appid,
                                      @RequestParam String openid,
                                      @RequestBody Map<String, Object> data) {
        try {
            String msgId = wxMpTemplateService.sendOrderMessage(openid, null, null, null);
            return R.ok(msgId);
        } catch (Exception e) {
            log.error("发送订单消息失败: {}", e.getMessage(), e);
            return R.fail("发送订单消息失败: " + e.getMessage());
        }
    }

    /**
     * 发送审核结果消息
     *
     * @param appid  公众号appid
     * @param openid 用户openid
     * @param data   审核数据
     * @return 发送结果
     */
    @PostMapping("/send-audit-result")
    @SwitchoverCheck
    public R<String> sendAuditResult(@PathVariable String appid,
                                     @RequestParam String openid,
                                     @RequestBody Map<String, Object> data) {
        try {
            String msgId = wxMpTemplateService.sendAuditResultMessage(openid, null, null);
            return R.ok(msgId);
        } catch (Exception e) {
            log.error("发送审核结果消息失败: {}", e.getMessage(), e);
            return R.fail("发送审核结果消息失败: " + e.getMessage());
        }
    }

    /**
     * 发送活动通知
     *
     * @param appid  公众号appid
     * @param openid 用户openid
     * @param data   活动数据
     * @return 发送结果
     */
    @PostMapping("/send-activity")
    @SwitchoverCheck
    public R<String> sendActivityNotification(@PathVariable String appid,
                                              @RequestParam String openid,
                                              @RequestBody Map<String, Object> data) {
        try {
            String msgId = wxMpTemplateService.sendActivityNotificationMessage(openid, null, null, null);
            return R.ok(msgId);
        } catch (Exception e) {
            log.error("发送活动通知失败: {}", e.getMessage(), e);
            return R.fail("发送活动通知失败: " + e.getMessage());
        }
    }
}
